﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Reflection;
using HslCommunication.Serial;

namespace HslCommunication.Profinet.LSIS
{
	/// <summary>
	/// XGB Cnet I/F module supports Serial Port. The address can carry station number information, for example: s=2;D100
	/// </summary>
	/// <remarks>
	/// <inheritdoc cref="T:HslCommunication.Profinet.LSIS.XGBCnetOverTcp" path="remarks" />
	/// </remarks>
	// Token: 0x02000082 RID: 130
	public class XGBCnet : SerialDeviceBase
	{
		/// <summary>
		/// Instantiate a Default object
		/// </summary>
		// Token: 0x060009DD RID: 2525 RVA: 0x0003AB00 File Offset: 0x00038D00
		public XGBCnet()
		{
			base.ByteTransform = new RegularByteTransform();
			base.WordLength = 2;
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.Station" />
		// Token: 0x170001A3 RID: 419
		// (get) Token: 0x060009DE RID: 2526 RVA: 0x0003AB25 File Offset: 0x00038D25
		// (set) Token: 0x060009DF RID: 2527 RVA: 0x0003AB2D File Offset: 0x00038D2D
		public byte Station { get; set; } = 5;

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.ReadByte(System.String)" />
		// Token: 0x060009E0 RID: 2528 RVA: 0x0003AB36 File Offset: 0x00038D36
		[HslMqttApi("ReadByte", "")]
		public OperateResult<byte> ReadByte(string address)
		{
			return ByteTransformHelper.GetResultFromArray<byte>(this.Read(address, 2));
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.Write(System.String,System.Byte)" />
		// Token: 0x060009E1 RID: 2529 RVA: 0x0001D14A File Offset: 0x0001B34A
		[HslMqttApi("WriteByte", "")]
		public OperateResult Write(string address, byte value)
		{
			return this.Write(address, new byte[]
			{
				value
			});
		}

		/// <inheritdoc />
		// Token: 0x060009E2 RID: 2530 RVA: 0x0003AB48 File Offset: 0x00038D48
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = XGBCnetOverTcp.BuildReadOneCommand(station, address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					result = OperateResult.CreateSuccessResult<bool[]>(SoftBasic.ByteToBoolArray(XGBCnetOverTcp.ExtractActualData(operateResult2.Content, true).Content, (int)length));
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.ReadCoil(System.String)" />
		// Token: 0x060009E3 RID: 2531 RVA: 0x0003ABD2 File Offset: 0x00038DD2
		public OperateResult<bool> ReadCoil(string address)
		{
			return this.ReadBool(address);
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.ReadCoil(System.String,System.UInt16)" />
		// Token: 0x060009E4 RID: 2532 RVA: 0x0003ABDB File Offset: 0x00038DDB
		public OperateResult<bool[]> ReadCoil(string address, ushort length)
		{
			return this.ReadBool(address, length);
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnet.WriteCoil(System.String,System.Boolean)" />
		// Token: 0x060009E5 RID: 2533 RVA: 0x0003ABE5 File Offset: 0x00038DE5
		public OperateResult WriteCoil(string address, bool value)
		{
			return this.Write(address, value);
		}

		/// <inheritdoc />
		// Token: 0x060009E6 RID: 2534 RVA: 0x0003ABEF File Offset: 0x00038DEF
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			return this.Write(address, new byte[]
			{
				value ? 1 : 0
			});
		}

		/// <inheritdoc />
		// Token: 0x060009E7 RID: 2535 RVA: 0x0003AC0C File Offset: 0x00038E0C
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool value)
		{
			XGBCnet.<WriteAsync>d__12 <WriteAsync>d__ = new XGBCnet.<WriteAsync>d__12();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<XGBCnet.<WriteAsync>d__12>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x060009E8 RID: 2536 RVA: 0x0003AC60 File Offset: 0x00038E60
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = XGBCnetOverTcp.BuildReadCommand(station, address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = XGBCnetOverTcp.ExtractActualData(operateResult2.Content, true);
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060009E9 RID: 2537 RVA: 0x0003ACD0 File Offset: 0x00038ED0
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			byte station = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = XGBCnetOverTcp.BuildWriteCommand(station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = XGBCnetOverTcp.ExtractActualData(operateResult2.Content, false);
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060009EA RID: 2538 RVA: 0x0003AD40 File Offset: 0x00038F40
		public override string ToString()
		{
			return string.Format("XGBCnet[{0}:{1}]", base.PortName, base.BaudRate);
		}
	}
}
